<template>
    <div class="p-2">
        <!-- <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div v-show="showSearch" class="mb-[10px]">
                <el-card shadow="hover">
                    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                        <el-form-item label="到期时间" prop="expirationTime">
                            <el-date-picker clearable v-model="dateRange" type="daterange"
                                value-format="YYYY-MM-DD" placeholder="请选择到期时间" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </transition> -->

        <el-card shadow="never">
            <template #header>
                <div style="color: #1ABC60;font-size: 14px;">短信目前还剩{{ userStore.sysTenantVo.smsBalance }}条</div>
            </template>
            <el-table v-loading="loading" :data="messagePayHistoryList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="购买时间" align="center" width="180">
                        <template #default="scope">
                            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                        </template>
                    </el-table-column>
    
                <el-table-column label="套餐类型" align="center" prop="packageType" >
                    <template #default="scope">
                        <span>{{ scope.row.packageType == 0 ? '提醒套餐' : '验证码套餐' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="有效期" align="center" prop="indate" >
                    <template #default="scope">
                        <span>{{ scope.row.indate }}年</span>
                    </template>
                </el-table-column>
                <el-table-column label="到期时间" align="center" prop="expirationTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.expirationTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="短信条数" align="center" prop="packageNum" />
                <el-table-column label="套餐余额" align="center" prop="smsBalance" />
            </el-table>

            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>
    </div>
</template>

<script setup name="MessagePayHistory" lang="ts">
import { listMessagePayHistory, getMessagePayHistory, delMessagePayHistory, addMessagePayHistory, updateMessagePayHistory } from '@/api/system/messagePayHistory';
import { MessagePayHistoryVO, MessagePayHistoryQuery, MessagePayHistoryForm } from '@/api/system/messagePayHistory/types';

import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const dateRange = ref<string[]>([]);
const messagePayHistoryList = ref<MessagePayHistoryVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const messagePayHistoryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
    visible: false,
    title: ''
});

const initFormData: MessagePayHistoryForm = {
    id: undefined,
    packageId: undefined,
    packageType: undefined,
    indate: undefined,
    expirationTime: undefined,
    packageNum: undefined,
    smsBalance: undefined,
}
const data = reactive<PageData<MessagePayHistoryForm, MessagePayHistoryQuery>>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        packageId: undefined,
        packageType: undefined,
        indate: undefined,
        expirationTime: undefined,
        packageNum: undefined,
        smsBalance: undefined,
        params: {
        }
    },
    rules: {
        id: [
            { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        packageId: [
            { required: true, message: "套餐id不能为空", trigger: "blur" }
        ],
        packageType: [
            { required: true, message: "套餐类型 0-提醒套餐 1-验证码套餐 不能为空", trigger: "change" }
        ],
        indate: [
            { required: true, message: "有效期不能为空", trigger: "blur" }
        ],
        expirationTime: [
            { required: true, message: "到期时间不能为空", trigger: "blur" }
        ],
        packageNum: [
            { required: true, message: "短信条数不能为空", trigger: "blur" }
        ],
        smsBalance: [
            { required: true, message: "短信余额不能为空", trigger: "blur" }
        ],
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询短信套餐购买记录列表 */
const getList = async () => {
    loading.value = true;
    const res = await listMessagePayHistory(proxy?.addDateRange(queryParams.value, dateRange.value));
    messagePayHistoryList.value = res.rows;
    total.value = res.total;
    loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
    reset();
    dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
    form.value = { ...initFormData };
    messagePayHistoryFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: MessagePayHistoryVO[]) => {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
    reset();
    dialog.visible = true;
    dialog.title = "添加短信套餐购买记录";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: MessagePayHistoryVO) => {
    reset();
    const _id = row?.id || ids.value[0]
    const res = await getMessagePayHistory(_id);
    Object.assign(form.value, res.data);
    dialog.visible = true;
    dialog.title = "修改短信套餐购买记录";
}

/** 提交按钮 */
const submitForm = () => {
    messagePayHistoryFormRef.value?.validate(async (valid: boolean) => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.id) {
                await updateMessagePayHistory(form.value).finally(() => buttonLoading.value = false);
            } else {
                await addMessagePayHistory(form.value).finally(() => buttonLoading.value = false);
            }
            proxy?.$modal.msgSuccess("操作成功");
            dialog.visible = false;
            await getList();
        }
    });
}

/** 删除按钮操作 */
const handleDelete = async (row?: MessagePayHistoryVO) => {
    const _ids = row?.id || ids.value;
    await proxy?.$modal.confirm('是否确认删除短信套餐购买记录编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
    await delMessagePayHistory(_ids);
    proxy?.$modal.msgSuccess("删除成功");
    await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
    proxy?.download('system/messagePayHistory/export', {
        ...queryParams.value
    }, `messagePayHistory_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
    getList();
});
</script>